package com.atguigu.eduservice.controller.frontcontroller;

import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.Result;
import com.atguigu.eduservice.entity.EduComment;
import com.atguigu.eduservice.entity.dto.comment.CommentAddInfoDTO;
import com.atguigu.eduservice.service.EduCommentService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 * 评论 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2024-07-09
 */
@RestController
@RequestMapping("/eduservice/frontComment")
@CrossOrigin
@Slf4j
public class EduCommentFrontController {
    @Autowired
    private EduCommentService eduCommentService;
    //前端的分页查询
    @PostMapping("getCommentFrontList/{page}/{limit}")
    public Result getCommentFrontList(@PathVariable long page, @PathVariable long limit){
        Page<EduComment> eduCommentPage = new Page<>(page, limit);
        Map<String,Object> map = eduCommentService.getCommentPage(eduCommentPage);
        return Result.ok().data("map",map);
    }
    //前端添加评论
    @PostMapping("addComment")
    public Result addComment(@RequestBody CommentAddInfoDTO eduCommentDTO,  HttpServletRequest request){
        log.info("我出来了 eduCommentDTO:{}",eduCommentDTO);
        String userId = JwtUtils.getMemberIdByJwtToken(request);
        int s = eduCommentService.addCommentInfo(eduCommentDTO, userId);
        if(s>0){
            return Result.ok();
        }
        return Result.error();
    }




}
